/**
 * 神州信息(2012)
 */
package net.abbp.report.view.cell.painter;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import net.abbp.core.util.StringUtils;
import net.abbp.report.model.Cell;
import net.abbp.report.view.cell.value.ImageValue;

import org.apache.commons.codec.binary.Base64;

/**
 * 图片单元绘制器
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        Date：2012-4-16
 */
public class ImageCellPainter implements ICellPainter {

    @Override
    public void paint(Graphics g, Rectangle rect, Cell cell) {
        Graphics2D g2 = (Graphics2D) g;
        String base64Data = ((ImageValue) cell.getValue()).getData();
        if (StringUtils.isEmpty(base64Data)) {
            return;
        }

        InputStream is = new ByteArrayInputStream(Base64.decodeBase64(base64Data));
        try {
            if (is != null) {
                g2.drawImage(ImageIO.read(is), (int) rect.getX(), (int) rect.getY(), (int) rect.getWidth(), (int) rect
                        .getHeight(), null);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g, Rectangle rect, Cell cell, int offsetY) {
        // TODO Auto-generated method stub

    }
}
